package com.blockmeta.bbs.immodule.greendao.repository;

import android.database.sqlite.SQLiteDatabase;

import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.dao.Todo;
import com.blockmeta.bbs.immodule.greendao.db.TodoDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class TodoRepo implements ITodoRepo {

    private TodoDao todoDao;
    private SQLiteDatabase db;
//    private static TodoRepo repo;
//
//    public static TodoRepo getInstance(Context context){
//        if(repo != null){
//            return repo;
//        }else{
//            synchronized (TodoRepo.class){
//                if(repo == null){
//                    repo = new TodoRepo(DBHelper.getInstance(context).getDaoSession().getTodoDao()
//                            , DBHelper.getInstance(context).getDb());
//                }
//                return repo;
//            }
//        }
//    }

    public TodoRepo(TodoDao todoDao, SQLiteDatabase imdb) {
        this.todoDao = todoDao;
        this.db = imdb;
    }

    /**
     * 代办消息去重复。服务端保障
     *
     * @param todo
     * @param whoId
     */
    @Override
    public synchronized void addTodo(Todo todo, long whoId) {
        try{
            Todo unique = todoDao.queryBuilder().where(TodoDao.Properties.TodoId.eq(todo.getTodoId())
                    , TodoDao.Properties.WhoId.eq(whoId)).unique();
            if (unique == null) {
                todoDao.insert(todo);
            } else {
                todo.setId(unique.getId());
                todoDao.update(todo);
            }
        }catch (Exception e){
            LogUtils.d("todo insert duplicate");
        }

    }

    /**
     * 获取当前用户下的最大todoId
     *
     * @param whoId
     * @return
     */
    @Override
    public int getMaxTodoIdByUserId(long whoId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(whoId));
        qb.limit(1);
        qb.orderDesc(TodoDao.Properties.TodoId);
        Todo m = qb.unique();
        int maxId = 0;
        if (m != null) {
            maxId = m.getId().intValue();
        }
        return maxId;
    }

    /**
     * 获取代办事项
     *
     * @param currentUserId
     * @return
     */
    @Override
    public List<Todo> getTodoList(long currentUserId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(currentUserId), TodoDao.Properties.Type.eq(Params.USER_PERSON));
        qb.orderDesc(TodoDao.Properties.CreateDate);
        List<Todo> todos = qb.list();

        return todos;
    }

    @Override
    public List<Todo> getGroupTodoList(long whoId, long circleId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(whoId)
                , TodoDao.Properties.GroupId.eq(circleId)
                , TodoDao.Properties.Type.eq(Params.USER_GROUP));
        qb.orderDesc(TodoDao.Properties.CreateDate);
        return qb.list();
    }


    @Override
    public Todo getTodoByTodoId(long todoId, long whoId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.TodoId.eq(todoId),
                TodoDao.Properties.WhoId.eq(whoId));
        List<Todo> todos = qb.list();
        if (todos != null && todos.size() > 0) {
            return todos.get(0);
        }
        return null;
    }




    @Override
    public void updateTodo(long todoId, boolean complete, boolean agree,
                           String handleMsg, long whoId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.TodoId.eq(todoId), TodoDao.Properties.WhoId.eq(whoId));
        Todo todo = qb.unique();
        if(todo != null){
            todo.setComplete(complete);
            todo.setAgree(agree);
            todo.setChecked(true);
            todo.setHandleDate((int) (System.currentTimeMillis() / 1000L));
            todo.setHandleMsg(handleMsg);
            todoDao.update(todo);
        }
    }



    @Override
    public long getAddFriendUnDoTodoCount(long uid) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uid));
        qb.where(TodoDao.Properties.Type.eq(Params.TODO_ADD_FRIEND));
        qb.where(TodoDao.Properties.Checked.eq(false));
        long count = qb.count();
        return count;
    }

    @Override
    public long countCircleUnCompleteTodo(long circleId, long uid) {

        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uid));
        qb.where(TodoDao.Properties.Type.eq(Params.TODO_JOIN_CIRCLE));
        qb.where(TodoDao.Properties.Checked.eq(false));
        qb.where(TodoDao.Properties.GroupId.eq(circleId));
        long count = qb.count();
        return count;
    }

    @Override
    public Todo getAddFriendLasteatUnCompleteTodo(long uid) {

        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uid));
        qb.where(TodoDao.Properties.Type.eq(Params.TODO_ADD_FRIEND));
        qb.where(TodoDao.Properties.Checked.eq(false));
        qb.orderDesc(TodoDao.Properties.CreateDate);
        List<Todo> todos = qb.limit(1).list();
        if(todos != null && todos.size() > 0){
            return todos.get(0);
        }
        else {
            return null;
        }
    }
    @Override
    public Todo getCircleLasteatUnCompleteTodo(long circleId, long uid) {

        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uid));
        qb.where(TodoDao.Properties.Type.eq(Params.TODO_JOIN_CIRCLE));
        qb.where(TodoDao.Properties.Checked.eq(false));
        qb.where(TodoDao.Properties.GroupId.eq(circleId));
        qb.orderDesc(TodoDao.Properties.CreateDate);
        List<Todo> todos = qb.limit(1).list();
        if(todos != null && todos.size() > 0){
            return todos.get(0);
        }
        else {
            return null;
        }
    }

    @Override
    public void deleteTodo(long todoId, long whoId) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.TodoId.eq(todoId) , TodoDao.Properties.WhoId.eq(whoId));
        try{
            Todo unique = qb.unique();
            if(unique != null){
                todoDao.deleteByKey(unique.getId());
            }
        }catch (Exception e){

        }
    }

    @Override
    public List<Todo> getAllTodoList(long uidInt) {
        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uidInt)
                );
        qb.orderDesc(TodoDao.Properties.CreateDate);
        return qb.list();

    }

    @Override
    public int countUnCompleteTodo(int uid) {

        QueryBuilder<Todo> qb = todoDao.queryBuilder();
        qb.where(TodoDao.Properties.WhoId.eq(uid));
        qb.where(TodoDao.Properties.Checked.eq(false));
        long count = qb.count();
        return (int) count;

    }
}
